# TvOSDScreen - This screen is used for TV playback OSD
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import gtk
import gobject
import clutter

from frontend.gui.widgets.arrow_texture import ArrowTexture

from frontend.user_event import UserEvent
from frontend.gui.screens.screen import Screen
from frontend.gui.transitions.transition import Transition

class TvOSDScreen(Screen):
    """
    TvOSDScreen
    
    This screen is displayed when TV is being watched. Usually this screen
    doesn't have any visible elements. User actions such as pause and rewind
    are displayed as on-screen-graphics.
    """
    
    def __init__(self, ui, theme, player):
        """
        Initialize screen
        @param ui: UserInterface object
        @param theme: Theme object
        @param player: MediaPlayer object
        """
        self.group = clutter.Group()
        Screen.__init__(self, ui.STAGE_WIDTH, ui.STAGE_HEIGHT)
        
        self.ui = ui
        self.theme = theme
        self.player = player

        self._create_aspect_ratio_textures()  
              
        ui.getStage().add(self.group)
    
    def _create_video_timeline(self):
        """
        Create video timeline widget. This widget displayes current position
        of the stream and video length
        """
        
    def _create_volume_overlay(self):
        """
        Create volume overlay widget. Displayed when user changes volume
        """
        
    def _create_aspect_ratio_textures(self):
        """
        Create textures that which are displayed when user changes aspect ratio.
        """
        #FIXME: We could use Cairo instead of pixmap graphics
        pixbuf1 = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("native_aspect_ratio"))
        pixbuf2 = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("widescreen_aspect_ratio"))
        pixbuf3 = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("zoom_aspect_ratio"))
        pixbuf4 = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("compromise_aspect_ratio"))
        texture_1 = clutter.Texture()
        texture_2 = clutter.Texture()
        texture_3 = clutter.Texture()
        texture_4 = clutter.Texture()
        texture_1.set_pixbuf(pixbuf1)
        texture_2.set_pixbuf(pixbuf2)
        texture_3.set_pixbuf(pixbuf3)
        texture_4.set_pixbuf(pixbuf4)
        self.group.add(texture_1)
        self.group.add(texture_2)
        self.group.add(texture_3)
        self.group.add(texture_4)
        self.aspect_textures = [texture_1, texture_2, texture_3, texture_4]
        self.timeout_key = None # This is used when canceling timeouts
        for texture in self.aspect_textures:
            texture.set_position((self.ui.STAGE_WIDTH - texture.get_width()) / 2, 
                                 int(self.ui.STAGE_HEIGHT / 1.5))
    
    def _hide_aspect_ratio_logo(self, number):
        """
        Hide aspect ratio texture. This is a callback function and
        shouldn't be called directly.
        """
        self.aspect_textures[number].hide()
        self.timeout_key = None
        return False
    
    def _display_aspect_ratio_logo(self, number):
        """
        Display aspect ratio logo on screen when ratio is changed.
        @param number: Number of the aspect ratio
        """
        if self.timeout_key is not None:
            gobject.source_remove(self.timeout_key) 
        for texture in self.aspect_textures:
            texture.hide()
        self.aspect_textures[number].show()
        self.timeout_key = gobject.timeout_add(2000, 
                                                 self._hide_aspect_ratio_logo, 
                                                 number)
    
    def get_group(self):
        """Return all actors of the screen."""
        return self.group

    def get_type(self):
        """Return screen type."""
        return Screen.OSD
    
    def get_name(self):
        """Return screen name (human readble)"""
        return "TelevisionOSD"
    
    def handle_user_event(self, event):   
        """ 
        Handle screen specific user events
        @param event: UserEvent object
        """
        type = event.get_type()
        
        # TV aspect ratio changed. Display aspect ratio logo on screen
        
        if type == UserEvent.USE_ASPECT_RATIO_1:
            self._display_aspect_ratio_logo(0)
        elif type == UserEvent.USE_ASPECT_RATIO_2:
            self._display_aspect_ratio_logo(1)
        elif type == UserEvent.USE_ASPECT_RATIO_3:
            self._display_aspect_ratio_logo(2)
        elif type == UserEvent.USE_ASPECT_RATIO_4:
            self._display_aspect_ratio_logo(3)
